'use strict';

const koa = require('koa');
const koaLogger = require('koa-logger');
const log4js = require('log4js');
const koaStatic = require('koa-static');
const koaRewrite = require('koa-rewrite');
const koaProxy = require('koa-proxy');
const path = require('path');

const logger = log4js.getLogger();


const app = new koa();

app.use(koaLogger());

app.use(koaRewrite('/', '/index.html'));
app.use(koaStatic(path.join(__dirname, '..', `client/src`)));
app.use(koaProxy({
  host: 'http://localhost:8080',
  match: /^\/game\//
}));


app.use(function* (next) {
  try {
    yield next;
  } catch (err) {
    this.status = 500;

    let errMsg = err.message || err;

    if (err && err.error && err.error.message) {
      errMsg = err.error.message;
    }

    this.body = {
      code: -1,
      message: errMsg,
    };
    logger.error(err);
  }
});


app.listen(8888, "0.0.0.0");

logger.info('Server started at http://localhost:8888');
